package com.infore.airsupervise.ui.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.SelectableHeaderHolder;
import com.infore.airsupervise.adapter.SelectableItemHolder;
import com.infore.airsupervise.bean.AwesomeIconItem;
import com.infore.airsupervise.bean.StationTreeE;
import com.infore.airsupervise.biz.StatisticsBiz;
import com.infore.airsupervise.biz.impl.StatisticsBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.ToastUtil;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 图标视图抽屉Fragment
 */
public class StatisticsDrawerFragment extends BaseFragment {

    @BindView(R.id.tree_container)
    FrameLayout treeContainer;
    @BindView(R.id.layout_progress)
    LinearLayout layoutProgress;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;

    private OnClickListener onClickListener;

    private AndroidTreeView tView;

    private StatisticsBiz statisticsBiz;
    private List<StationTreeE> datas;

    public interface OnClickListener{
        void onConfirmClick(String  mn);
        void onCancelClick();
    }

    public void setOnClickListener( OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }

    public StatisticsDrawerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_statistics_drawer, container, false);
        unbinder = ButterKnife.bind(this, view);

        statisticsBiz = new StatisticsBizImpl();
        getTreeData();
        return view;
    }

    /**
     * 获取数据
     */
    private void getTreeData() {
       Call  call = statisticsBiz.getStationTree(new OnServerResponseListener<List<StationTreeE>>() {
            @Override
            public void onSucceed(List<StationTreeE> result, String desc) {
                layoutProgress.setVisibility(View.GONE);
                layoutContent.setVisibility(View.VISIBLE);
                datas = result;
                initTreeNode();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                layoutProgress.setVisibility(View.GONE);
                ToastUtil.showLong(getContext(), "站点列表获取失败");
            }
        });
        requests.add(call);
    }

    /**
     * 初始化节点
     */
    private void initTreeNode() {
        if (null == datas) {
            return;
        }

        TreeNode root = TreeNode.root();

        for (StationTreeE fatherSta : datas) {
            // 创建父节点
            TreeNode fatherNode = new TreeNode(new AwesomeIconItem(fatherSta.getName(), fatherSta))
                    .setViewHolder(new SelectableHeaderHolder(getContext()));

            List<StationTreeE> childStaList = fatherSta.getStationList();
            if (null != childStaList) {
                // 创建子节点
                for (StationTreeE childSta : childStaList) {
                    TreeNode childNode = new TreeNode(new AwesomeIconItem(childSta.getName(), childSta))
                            .setViewHolder(new SelectableItemHolder(getContext()));
                    // 把子节点加入父节点
                    fatherNode.addChild(childNode);
                }
            }

            // 把父节点加入根节点
            root.addChild(fatherNode);
        }

        tView = new AndroidTreeView(getContext(), root);
        tView.setDefaultAnimation(true);
        tView.setSelectionModeEnabled(true);
        treeContainer.addView(tView.getView());
    }

    /**
     * 获取已选站点列表
     *
     * @return
     */
    public List<StationTreeE> getSelectedStations() {
        if (null == tView) {
            return Collections.EMPTY_LIST;
        }

        List<AwesomeIconItem> selItems = tView.getSelectedValues(AwesomeIconItem.class);
        List<StationTreeE> result = new ArrayList<>();
        for (AwesomeIconItem item : selItems) {
            StationTreeE sta = (StationTreeE) item.getObj();
            if (null == sta.getStationList() || sta.getStationList().size() == 0) {
                // 只加入子节点或没有子节点的父节点
                result.add(sta);
            }
        }
        return result;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.btn_confirm, R.id.btn_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                if(onClickListener != null){
                    onClickListener.onConfirmClick(getMnList());
                }
                break;
            case R.id.btn_cancel:
                tView.deselectAll();
                if(onClickListener != null){
                    onClickListener.onCancelClick();
                }
                break;
        }

    }

    /**
     * 获取选中的站点mn号
     */
    private String  getMnList(){
        List<StationTreeE> selected = getSelectedStations();
        if (null == selected || selected.size() < 2) {
            ToastUtil.showLong(getContext(), "至少选择2个站点");
            return null;
        }

        if (selected.size() > 10) {
            ToastUtil.showLong(getContext(), "最多选择10个站点");
            return null;
        }

        StringBuffer mnSB = new StringBuffer();
        for (StationTreeE sta : selected) {
            if (mnSB.length() != 0) {
                mnSB.append(",");
            }
            mnSB.append(sta.getMn());
        }

        // 设置选择MN号
        return  mnSB.toString();
    }
}
